package com.lianwei.hospital.controller;

import com.lianwei.hospital.Service.BaseProvinceService;
import com.lianwei.hospital.Service.Impl.UserInfoImpl;
import com.lianwei.hospital.Service.UserService;
import com.lianwei.hospital.module.entity.BaseProvince;
import com.lianwei.hospital.module.entity.DoctorInfo;
import com.lianwei.hospital.module.entity.UserInfo;
import com.lianwei.hospital.module.vo.DoctorVO;
import com.lianwei.hospital.module.vo.UserVO;
import com.lianwei.hospital.utils.PageUtil;
import com.lianwei.hospital.utils.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.DigestUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.List;

@RestController
@RequestMapping("/user")
public class UserInfoController {
    @Autowired
    private UserService userService;

    @Autowired
    private BaseProvinceService baseProvinceService;

    @PostMapping("/login")
    public Result<UserInfo> login(@RequestParam("username") String id, @RequestParam("password") String password, HttpSession session) throws Exception{
        UserInfo userInfo= userService.login(id, password);
        if (userInfo!=null){
            session.setAttribute("userInfo",userInfo);
//            String res= JwtUtils.sign(id, String.valueOf(doctorInfo.getId()));
//            System.out.println(res);
            return Result.buildResult(Result.Status.OK,userInfo);
        }
        return Result.buildResult(Result.Status.BAD_REQUEST);
    }

    @GetMapping("/selectById")
    public Result<UserInfo> tesSelect(String i) throws Exception {
        System.out.println(i+"\n\n");
        UserInfo userInfo = userService.selectByPrimaryKey(i);
        return Result.buildResult(Result.Status.OK,userInfo);
    }

//    插入数据
    @PostMapping("/insert")
    public Result<List<UserInfo>> Insert(@RequestBody UserInfo userInfo) throws Exception {
        System.out.println("插入时"+userInfo);
        BaseProvince baseProvince = baseProvinceService.selectByName(userInfo.getProvince());
        userInfo.setProvinceId(baseProvince.getId());
        userInfo.setBaseProvince(baseProvince);
        if(userService.insertUser(userInfo)){
            List<UserInfo> userInfoList = userService.selectAll();
            return Result.buildResult(Result.Status.OK,userInfoList);
        }
        return Result.buildResult(Result.Status.BAD_REQUEST);
    }

    @PostMapping("deleteById")
    public Result<List<UserInfo>> Delete(@RequestBody UserInfo userInfo) throws Exception {
        System.out.println("这里是删除信息"+userInfo);
        if(userService.deleteById(userInfo.getUId())){
            List<UserInfo> userInfoList = userService.selectAll();
            return Result.buildResult(Result.Status.OK,userInfoList);
        }
        return Result.buildResult(Result.Status.BAD_REQUEST);
    }

    @PutMapping
    public Result<List<UserInfo>> Update(@RequestBody UserInfo userInfo) throws Exception {
        System.out.println("\n更新时信息"+userInfo);
        BaseProvince baseProvince = baseProvinceService.selectByName(userInfo.getProvince());
        userInfo.setProvinceId(baseProvince.getId());
        userInfo.setBaseProvince(baseProvince);
        if(userService.updateById(userInfo)){
            List<UserInfo> userInfoList = userService.selectAll();
            return Result.buildResult(Result.Status.OK,userInfoList);
        }
        return Result.buildResult(Result.Status.BAD_REQUEST);
    }

    @GetMapping("/selectAll")
    public Result<PageUtil<UserInfo>> list( UserVO query) throws Exception {
        PageUtil<UserInfo> pageUtil=userService.findPage(query);
        return Result.buildResult(Result.Status.OK,pageUtil);
    }
    @GetMapping("selectBySession")
    public Result<UserInfo>  getUser()throws Exception{
        return Result.buildResult(Result.Status.OK,userService.selectByPrimaryKey("2"));
    }
}
